package mv.wap;

import mv.io.SimpleProperties;
import mv.util.EncryptionUtil;

import java.util.List;
import java.util.ArrayList;

import com.google.gdata.util.common.base.CharEscaper;
import com.google.gdata.util.common.base.CharEscapers;

/**
 * Class description...
 *
 * @author mvu
 */
public class Page extends Element{
  String _title;
  List<Link> _links;
  public static final String HOME = "Home";

  public Page(String title, Element... children) {
    super(children);
    _title = title;
    _links = new ArrayList<Link>();
  }

  @Override
  public Page addElement(Element element) {
    super.addElement(element);
    addLink(element);
    return this;
  }

  private void addLink(Element element){
    if (element instanceof Link) {
      if(_links == null){
        _links = new ArrayList<Link>();
      }
      _links.add((Link) element);
    }else{
      for(Element child : element.getChildren()){
        addLink(child);
      }
    }
  }

  public String toHtml(Element parent) {
    StringBuilder html = new StringBuilder()
            .append("<html><head><script language=\"javascript\" src=\"global.js\"></script></head><body><h2>")
            .append(_title).append("</h2><a href=\"javascript:logout()\">Logout</a>");

    StringBuilder content = new StringBuilder();
    for (Element e : getChildren()) {
      content.append(e.toHtml(this));
    }
    html.append(content.toString())
            .append("</body></html>");
    return html.toString();
  }

  public String getTitle() {
    return _title;
  }

  public String toHtml() {
    return toHtml(null);
  }

  public List<Link> getLinks() {
    return _links;
  }

  public String toString() {
    return toHtml(null);
  }
}
